package com.gt.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 *
 */
public class XyLocalDateUtil {
    public static final String FORMAT_01 = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_02 = "yyyyMMdd";
    public static final String FORMAT_03 = "yyyy-MM-dd";
    public static final String FORMAT_04 = "yyyyMMddHHmmss";
    public static final String FORMAT_05 = "yyyy年MM月dd日";
    public static final String FORMAT_06 = "yyyy.MM.dd";
    public static final String FORMAT_07 = "yyyy-MM-dd'T'HH:mm:ssX";

    /**
     * 时间戳转换成时间
     * @param time
     * @return
     */
    public static LocalDateTime toTime(long time) {
        return Instant.ofEpochMilli(time).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
    }

    /**
     * LocalDateTime转换成时间戳
     * @param dateTime
     * @return
     */
    public static long toMilli(LocalDateTime dateTime){
        return dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static void main(String[] args) {
        System.out.println(format(LocalDateTime.now(),FORMAT_01));
    }

    /**
     * 格式化
     * @param dateTime
     * @param pattern
     * @return
     */
    public static String format(LocalDateTime dateTime, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(dateTime);
    }
    /**
     * wjh
     * 2021/6/11
     * 将时间戳字符串转成LocalDateTime类型
    */
    public static LocalDateTime parse(String dateTime, String pattern) {
        return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(pattern));
    }
    /**
     * wjh
     * 2021/11/16
     * 添加秒数
     */
    public static LocalDateTime addSeconds(int seconds) {
        LocalDateTime time = LocalDateTime.now();
        return time.plusSeconds(seconds);
    }
}
